package com.yunsc.order.handler.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunsc.order.entity.YunscOrderEntity;
import com.yunsc.order.handler.RedisDelayQueueHandle;
import com.yunsc.order.handler.exception.DelayExceptionHandler;
import com.yunsc.order.sevice.YunscOrderService;
import com.yunsc.store.service.YunscUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 订单超时未收货处理器
 * @date: 2024-06-13 20:52
 */
@Slf4j
@Component("orderTimeoutNotReceived")
public class OrderTimeoutNotReceived  implements RedisDelayQueueHandle<Map> {

    @Resource
    private YunscOrderService orderService;
    @Resource
    private YunscUserService userService;


    @Override
    @Transactional
    @DelayExceptionHandler
    public void execute(Map map) {
        log.info("订单超时未收货处理器开始执行，订单号：{}", map.get("orderNo"));
        // 订单超时未收货，系统默认自动取消订单处理业务...
        Integer orderId = (Integer) map.get("orderId");
        YunscOrderEntity orderEntity = orderService.getOne(new LambdaQueryWrapper<YunscOrderEntity>()
                .eq(YunscOrderEntity::getOrderId, orderId).eq(YunscOrderEntity::getIsDelete, 0));
        if (orderEntity == null) {
            return;
        }
        if (orderEntity.getReceiptStatus() == 20) {
            return;
        }
        // 20已收货
        orderEntity.setReceiptStatus(20);
        // 30已完成
        orderEntity.setOrderStatus(30);
        orderService.updateById(orderEntity);

        // 赠送积分
        if (orderEntity.getPointsBonus() > 0){
            userService.updateUserPoints(orderEntity.getUserId(), orderEntity.getPointsBonus(), "订单收货赠送积分");
        }

        log.info("订单超时未收货处理器执行完成，订单号：{}", map.get("orderNo"));
    }


}
